package de.hsos.bose.skillshare.view;

import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.JTableHeader;

import de.hsos.bose.skillshare.model.core.DataAccess;
import de.hsos.bose.skillshare.model.core.table.DetailTableModel;
import de.hsos.bose.skillshare.model.core.table.GroupTableModel;
import de.hsos.bose.skillshare.model.objects.SkillDetail;
import de.hsos.bose.skillshare.model.objects.SkillGruppe;

public class SkillsManagePanel extends BasePanel {

	private static final long serialVersionUID = 1L;

	private static String resSave = "Save";
	private static String resAdd = "Add";
	private static String resDelete = "Delete";

	private GroupTableModel groupTableModel;
	private DetailTableModel detailTableModel;

	private JTable groupTable;
	private JTable detailTable;
	private JButton btnGroupAdd;
	private JButton btnGroupSave;
	private JButton btnGroupDelete;
	private JButton btnDetailAdd;
	private JButton btnDetailSave;
	private JButton btnDetailDelete;
	private JTextField txtGroupName;
	private JTextField txtGroupDescription;
	private JTextField txtDetailName;
	private JTextField txtDetailDescription;
	protected JScrollPane groupScroller;
	protected JScrollPane detailScroller;
	private SkillsManagePanel me;

	private DefaultCellEditor dceGroupName;
	private DefaultCellEditor dceDetailName;
	private DefaultCellEditor dceGroupDescription;
	private DefaultCellEditor dceDetailDescription;

	protected DataAccess da;

	private SkillGruppe currentGroup;
	private SkillDetail currentDetail;

	public SkillsManagePanel() {
		da = new DataAccess();
		me = this;
		initGroupTable();
	}

	private void initGroupTable() {
		Container pane = this;
		Insets insets = pane.getInsets();

		setLayout(null);

		btnGroupAdd = new JButton(resAdd);
		btnGroupAdd.setBounds(10 + insets.left, 200 + insets.top, 100, 25);
		btnGroupAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				groupTableModel.addEmptyRow();
			}
		});
		btnGroupSave = new JButton(resSave);
		btnGroupSave.setBounds(120 + insets.left, 200 + insets.top, 100, 25);
		btnGroupSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int current = groupTable.getSelectedRow();
				for (SkillGruppe group : groupTableModel.getDirty()) {
					da.updateSkillGroup(group);
				}
				for (SkillGruppe group : groupTableModel.getNew()) {
					if (group.getSkillGruppenName() != null) {
						if (!group.getSkillGruppenName().trim().isEmpty()) {
							da.insertSkillGruppe(group);
						}
					}
				}
				groupTableModel.loadGroups();
				groupTable.getSelectionModel().setSelectionInterval(current,
						current);
			}
		});
		btnGroupDelete = new JButton(resDelete);
		btnGroupDelete.setBounds(230 + insets.left, 200 + insets.top, 100, 25);
		btnGroupDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (currentGroup != null) {
					if (da.deleteSkillGroup(currentGroup.getSkillGruppenID()) == 23503) {
						JOptionPane.showMessageDialog(me,
								"This group is still assigned to details",
								"Group can not be deleted",
								JOptionPane.ERROR_MESSAGE);
					} else {
						currentGroup = null;
						groupTableModel.loadGroups();
					}
				}
			}
		});

		btnDetailAdd = new JButton(resAdd);
		btnDetailAdd.setBounds(10 + insets.left, 425 + insets.top, 100, 25);
		btnDetailAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (currentGroup != null) {
					detailTableModel.addEmptyRow(currentGroup
							.getSkillGruppenID());
				}
			}
		});
		btnDetailSave = new JButton(resSave);
		btnDetailSave.setBounds(120 + insets.left, 425 + insets.top, 100, 25);
		btnDetailSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int current = detailTable.getSelectedRow();
				for (SkillDetail detail : detailTableModel.getDirty()) {
					da.updateSkillDetail(detail);
				}
				for (SkillDetail detail : detailTableModel.getNew()) {
					if (detail.getSkillDetailName() != null) {
						if (!detail.getSkillDetailName().trim().isEmpty()) {
							da.insertSkillDetail(detail);
						}
					}
				}
				detailTableModel.loadEmpoyees(currentGroup.getSkillGruppenID());
				detailTable.getSelectionModel().setSelectionInterval(current,
						current);
			}
		});
		btnDetailDelete = new JButton(resDelete);
		btnDetailDelete.setBounds(230 + insets.left, 425 + insets.top, 100, 25);
		btnDetailDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (currentDetail != null) {
					if (da.deleteSkillDetail(currentDetail.getSkillDetailID()) == 23503) {
						JOptionPane.showMessageDialog(me,
								"This detail is still assigned to Skills",
								"Detail can not be deleted",
								JOptionPane.ERROR_MESSAGE);
					} else {
						currentDetail = null;
						detailTableModel.loadEmpoyees(currentGroup
								.getSkillGruppenID());
					}
				}
			}
		});

		txtGroupName = new JTextField();
		txtGroupDescription = new JTextField();

		txtDetailName = new JTextField();
		txtDetailDescription = new JTextField();

		groupTableModel = new GroupTableModel();
		detailTableModel = new DetailTableModel();
		groupTableModel.loadGroups();

		groupTable = new JTable(groupTableModel);
		detailTable = new JTable(detailTableModel);

		groupTable.setDragEnabled(false);
		detailTable.setDragEnabled(false);
		groupTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		detailTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		groupTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting()) {
							if (e.getSource() == groupTable.getSelectionModel()
									&& groupTable.getRowSelectionAllowed()) {
								dceGroupName.stopCellEditing();
								dceGroupDescription.stopCellEditing();
								dceDetailName.stopCellEditing();
								dceDetailDescription.stopCellEditing();
								currentGroup = groupTableModel
										.getSkillGruppe(((DefaultListSelectionModel) e
												.getSource())
												.getLeadSelectionIndex());
								currentDetail = null;
								detailTableModel.loadEmpoyees(currentGroup
										.getSkillGruppenID());
							}
						}
					}
				});
		detailTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting()) {
							if (e.getSource() == detailTable
									.getSelectionModel()
									&& detailTable.getRowSelectionAllowed()) {
								currentDetail = detailTableModel
										.getSkillDetail(((DefaultListSelectionModel) e
												.getSource())
												.getLeadSelectionIndex());

							}
						}
					}

				});
		groupTable.setRowHeight(20);
		groupTable.getColumnModel().getColumn(GroupTableModel.GROUP_NAME)
				.setPreferredWidth(200);

		dceGroupName = new DefaultCellEditor(txtGroupName);
		dceGroupName.setClickCountToStart(1);
		dceGroupName.addCellEditorListener(new CellEditorListener() {
			@Override
			public void editingStopped(ChangeEvent arg0) {
				System.out.println("GroupName editingStopped");
				if (currentGroup != null) {
					currentGroup.setDirty(true);
				}
			}

			@Override
			public void editingCanceled(ChangeEvent arg0) {
				System.out.println("GroupName editingCanceled");
				if (currentGroup != null) {
					currentGroup.setDirty(true);
				}
			}
		});

		groupTable.getColumnModel().getColumn(GroupTableModel.GROUP_NAME)
				.setCellEditor(dceGroupName);

		groupTable.getColumnModel()
				.getColumn(GroupTableModel.GROUP_DESCRIPTION)
				.setPreferredWidth(550);

		dceGroupDescription = new DefaultCellEditor(txtGroupDescription);
		dceGroupDescription.setClickCountToStart(1);
		dceGroupDescription.addCellEditorListener(new CellEditorListener() {
			@Override
			public void editingStopped(ChangeEvent arg0) {
				System.out.println("GroupDescription editingStopped");
				if (currentGroup != null) {
					currentGroup.setDirty(true);
				}
			}

			@Override
			public void editingCanceled(ChangeEvent arg0) {
				System.out.println("GroupDescription editingCanceled");
				if (currentGroup != null) {
					currentGroup.setDirty(true);
				}
			}
		});

		groupTable.getColumnModel()
				.getColumn(GroupTableModel.GROUP_DESCRIPTION)
				.setCellEditor(dceGroupDescription);

		detailTable.setRowHeight(20);
		detailTable.getColumnModel().getColumn(DetailTableModel.DETAIL_NAME)
				.setPreferredWidth(200);

		dceDetailName = new DefaultCellEditor(txtDetailName);
		dceDetailName.setClickCountToStart(1);
		dceDetailName.addCellEditorListener(new CellEditorListener() {
			@Override
			public void editingStopped(ChangeEvent arg0) {
				System.out.println("DetailName editingStopped");
				if (currentDetail != null) {
					currentDetail.setDirty(true);
				}
			}

			@Override
			public void editingCanceled(ChangeEvent arg0) {
				System.out.println("DetailName editingCanceled");
				if (currentDetail != null) {
					currentDetail.setDirty(true);
				}
			}
		});

		detailTable.getColumnModel().getColumn(DetailTableModel.DETAIL_NAME)
				.setCellEditor(dceDetailName);

		detailTable.getColumnModel()
				.getColumn(DetailTableModel.DETAIL_DESCRIPTION)
				.setPreferredWidth(550);

		dceDetailDescription = new DefaultCellEditor(txtDetailDescription);
		dceDetailDescription.setClickCountToStart(1);
		dceDetailDescription.addCellEditorListener(new CellEditorListener() {
			@Override
			public void editingStopped(ChangeEvent arg0) {
				System.out.println("DetailDescription editingStopped");
				if (currentDetail != null) {
					currentDetail.setDirty(true);
				}
			}

			@Override
			public void editingCanceled(ChangeEvent arg0) {
				System.out.println("DetailDescription editingCanceled");
				if (currentDetail != null) {
					currentDetail.setDirty(true);
				}
			}
		});

		detailTable.getColumnModel()
				.getColumn(DetailTableModel.DETAIL_DESCRIPTION)
				.setCellEditor(dceDetailDescription);

		groupTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		detailTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		groupTable.setPreferredScrollableViewportSize(groupTable
				.getPreferredSize());
		detailTable.setPreferredScrollableViewportSize(detailTable
				.getPreferredSize());

		JTableHeader groupHeader = groupTable.getTableHeader();
		groupHeader.setResizingAllowed(false);
		groupHeader.setReorderingAllowed(false);

		JTableHeader detailHeader = detailTable.getTableHeader();
		detailHeader.setResizingAllowed(false);
		detailHeader.setReorderingAllowed(false);

		groupScroller = new JScrollPane(groupTable);
		groupScroller.setBounds(10 + insets.left, 10 + insets.top, 768, 185);

		detailScroller = new JScrollPane(detailTable);
		detailScroller.setBounds(10 + insets.left, 235 + insets.top, 768, 185);

		this.add(groupScroller);
		this.add(detailScroller);
		this.add(btnGroupAdd);
		this.add(btnGroupSave);
		this.add(btnGroupDelete);
		this.add(btnDetailAdd);
		this.add(btnDetailSave);
		this.add(btnDetailDelete);

	}

}
